import { Modal } from 'antd';
import { queryDictData, queryPerson } from '@/utils/common';
import AdSelect from '@/components/AdSelect';
import AdSearch from '@/components/AdSearch';
import prompt from '@/components/Prompt';

export const formate = 'YYYY-MM-DD HH:mm:ss';
export const formateNOSecond = 'YYYY-MM-DD HH:mm';
const { confirm } = Modal;

export const codes = {
  select: 'ENDORSELISTSELECT',
  showDetail: 'ENDORSELIST_VIEW',
  confirm:'ENDORSELIST_CONF',
  remove:'ENDORSELIST_DEL',
  add: 'ENDORSELIST_ADD',
  addEndorse:'ENDORSE_ADD',
  edit: 'ENDORSELIST_UPD',
  upload: 'ENDORSELIST_IMPORT',
  page: 'ENDORSELIST',
  bill: 'ENDORSELIST_GENERATE',
  import:'ENDORSELIST_IMPORT',
  export:'ENDORSELIST_EXPORT',
};
// UNSEALED 草稿、 SUBMITTED 已提交、 CONFIRM 已确认  CANCEL 作废
export const archivesStatusList = [
  { code: 'CANCEL', value: '作废' },
  { code: 'UNSEALED', value: '草稿' },
  { code: 'CONFIRM', value: '已确认' },
  { code: 'SUBMITTED', value: '已提交' },
];
//提派点： sendSome
export const sendSomeList = [{ code: '1', value: '1' }, { code: '2', value: '2' }];
// 车次类型
export const trainType = [
  { code: 'NORMAL', value: '正常运输' },
  { code: 'EMPTY', value: '放空' },
  { code: 'SHORT', value: '短途' },
  { code: 'LONG', value: '长途' },
];
export const emptyTypeList = [
  { code: 'EMPTY', value: '放空补贴' },
  { code: 'NIGHTSHIFT', value: '过夜补贴' },
  { code: 'CABINET', value: '深圳柜补贴' },
  { code: 'WEEKEND', value: '周末开工补贴' },
];

export const allUrl = {
  driverList: 'tms/tms-papers/selectDriverList_s',
  driverDetail: 'tms/tms-driver/viewDetails',
  companyList: 'tms/tms-bill/selectSpplierList_s',
  userList: 'tms/tms-bill/selectUserList_s',
  supplierList: 'tms/tms-bill/selectSpplierList_s',
  supplierDetail: 'mds/d-customer/viewCompany',
  userDetail: 'mds/mds-user/viewDetails',
};

export const routeUrl = {
  add: '/carDaily/EndorseList/addEndorse',
  edit: '/carDaily/EndorseList/editEndorse',

  editOneTeam: '/carAccount/ArchivesList/editArchivesOneTeam',
  editHK: '/carAccount/ArchivesList/editArchivesHK',
  editEmpty: '/carAccount/ArchivesList/editArchivesEmpty',
};

export const allDispatchType = {
  // 违章记录
  list: 'Endorse/selectEndorseList',
  detail: 'Endorse/selectEndorseDetail',
  operate:'Endorse/endorseOperate',
  remove: 'Endorse/deleteEndorse',
  confirm: 'Endorse/confirmEndorse',
  commit: 'Endorse/commitEndorse',
  detailInfoDownload:'Endorse/detailInfoDownload',
  exportEndorse:'Endorse/exportEndorse',
 
  oneTeamoperate: 'Archives/archivesOneTeamOperate',

  trial: 'Archives/trialFeeArchives',
  cancel: 'Archives/cancelArchives',
  pull: 'Archives/pullArchivesByP2',
  emptyArchives: 'Archives/emptyArchivesOperate',
  selectEmptyDetails: 'Archives/selectEmptyDetails',
  toLocal: 'Archives/toLocal',
  //车次
  trainInfoList: 'Archives/trainInfoList',
  trainInfoDetails: 'Archives/trainInfoDetails',
  trainInfoOperate: 'Archives/trainInfoOperate',
  trainInfoOneTeamOperate: 'Archives/trainInfoOneTeamOperate',
  deleteTrains: 'Archives/deleteTrains',
  //杂费
  incidentalInfoList: 'Archives/incidentalInfoList',
  pullIncidentals: 'Archives/pullIncidentals',
  //加油
  oilInfoList: 'Archives/oilInfoList',
  oilInfoDetails: 'Archives/oilInfoDetails',
  oilInfoOperate: 'Archives/oilInfoOperate',
  oilBindInfoList: 'Archives/oilBindInfoList',
  oilBindInfoOperate: 'Archives/oilBindInfoOperate',
  removeOilInfo: 'Archives/removeOilInfo',
  oilCardList: 'Archives/oilCardList',
  calHundredConsump: 'Archives/calHundredConsump',
  // 压夜
  pressureInfoList: 'Archives/pressureInfoList',

  ownCompany: 'component/queryOwnCompany',
  
  value: 'Archives/allValus',
  selectFileList: 'Archives/selectFileList',
  getStartMileage: 'Archives/getStartMileage',
  getEtcStartAmount: 'Archives/getEtcStartAmount',
  
};

export function renderTableAdSelect({ key, data, value, props, type }) {
  let params = { onlyRead: true, value };

  if (key) {
    const { dictObject } = props;
    params = { data: dictObject[key], payload: { code: [key] }, ...params };
  } else {
    params = { data, ...params };
  }
  let show = { id: 'code', name: 'value' };
  if (type == 'train') {
    show = { id: 'id', name: 'trainNo' };
  }
  params.show = show;
  return <AdSelect {...params} />;
}

export function renderTableAdSearch({ value, props }) {
  if (!value || !searchValue) return '';
  const { searchValue } = props;
  const params = {
    onlyRead: true,
    value: searchValue[value],
    label: 'loginName',
    name: 'sysName',
  };
  return <AdSearch {...params} />;
}

export function selectEndorseList({ payload = {}, props } = {}) {
  const { dispatch, searchValue } = props;
  dispatch({
    type: allDispatchType.list,
    payload,
    callback: data => {
      if (!data) return;
      queryPerson({ data, props: props, url: allUrl.userList });
    },
  });
  
}

export function selectArchivesDetailAndInfo({ type, payload, props, callback }) {
  const { dispatch } = props;
  dispatch({
    type,
    payload,
    callback: data => {
      if (!data) return;
      callback && callback(data);
    },
  });
}

export function saveAllValues({ payload = {}, props } = {}) {
  const { dispatch } = props;
  dispatch({
    type: 'Endorse/allValus',
    payload,
  });
}

//时间判断：  date1:小日期   date2:大日期
export function DateMinus(date1, date2) {
  if (date1 && date2) {
    var sdate = new Date(date1);
    var now = new Date(date2);
    var days = Number(now.getTime()) - Number(sdate.getTime());
    var day = parseFloat(days / (1000 * 60 * 60 * 24));
    return day.toString();
  }
}
/*
   *  确认违章时需进行校验：
   所有字段不能为空：
      1）ETC发车金额如不为空或0,则收车金额不允许为空或0
      2）车次字段不能为空
 */
export function checkConfirm({ props, id, type, isHK, callback }) {
  const {
    trainInfoList,
    oilInfoList,
    pressureInfoList,
    form: { getFieldValue },
  } = props;
  let tipsText = '';

  const trainList = trainInfoList[id] ? trainInfoList[id].list || [] : [];
  const pressureList = pressureInfoList[id] ? pressureInfoList[id].list || [] : [];
  const oilList = oilInfoList[id] ? oilInfoList[id].list || [] : [];
  const etcSA = getFieldValue('etcStartAmount');
  const etcEA = getFieldValue('etcEndAmount');
  if (etcSA && !etcEA) {
    tipsText = '收车余额 不允许为空或0；';
  }
  //校验行车档案
  const unit = {
    checkFields: (list, fields, textType) => {
      let textFields = '';
      list.map((item, i) => {
        let tipsStr = [];
        for (let k in item) {
          if (!item[k] && fields[k]) {
            tipsStr.push(fields[k]);
          }
        }
        if (tipsStr.length > 0) {
          // tipsText = `${tipsText}${textType}序号${i + 1}：${tipsStr.join(',')}不允许为空；`;
          textFields = `${textFields}序号${i + 1}：${tipsStr.join(',')}不允许为空；`;
        }
      });
      if (textFields) {
        tipsText = tipsText + `<div>${textType}${textFields}</div>`;
      }
    },
    titleTips: val => {
      switch (val) {
        case 'confirm':
          return '确认';
          break;
        case 'trial':
          return '试算';
          break;
        case 'commit':
          return '提交';
          break;
        default:
          return;
      }
    },
  };
  //校验车次
  if (!isHK) {
    if (trainList.length == 0) {
      tipsText = tipsText + '车次数据不能为空。';
    } else {
      const fields = {
        trainNo: '车次号',
        weight: '货重',
        cabinetType: '柜型',
        cartHanging: '车挂',
        driverName: '司机',
        origin: '始发地',
        destination: '目的地',
        transportStartTime: '发车时间',
        transportEndTime: '收车时间',
        startMileage: '发车里程',
        collectMileage: '收车里程',
      };
      // unit.checkFields(trainList, fields, '车次信息：');
    }
  }
  if (!isHK) {
    if (oilList.length == 0) {
      tipsText = tipsText + '加油数据不能为空。';
    } else {
      const fields = {
        oilAmount: '加油量',
      };
      unit.checkFields(oilList, fields, '加油记录：');
    }
  }

  if (pressureList.length > 0) {
    const fields = {
      driverName: '司机',
    };
    if (!isHK) fields.trainId = '车次号';
    unit.checkFields(pressureList, fields, '压夜记录：');
  }

  if (tipsText) {
    prompt({
      content: (
        <div dangerouslySetInnerHTML={{ __html: `${unit.titleTips(type)}时：` + tipsText }}></div>
      ),
      type: 'error',
    });
    return;
  }
  if (type == 'commit') {
    confirm({
      title: '确认提交？',
      content: ' ',
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk: () => {
        callback && callback();
      },
    });
  } else {
    callback && callback();
  }
}

export const columnsTrain = [
  {
    title: '车次号',
    dataIndex: 'trainNo',
    width: '100%',
  },
];

// INLAND("INLAND", "国内车（默认）"),
// HK("HK", " 中港车-过港"),
// HK_NONSTOP("HK_NONSTOP","中港车-直达"),
// LOCAL("LOCAL","本地local"),
// EMPTY("EMPTY","放空"),
// ONE_TEAM("ONE_TEAM","一车队");
